<%= page_title "Edit #{@project.name} Kubernetes Resources" %>

<section>
  <%= form_tag update_many_project_kubernetes_deploy_group_roles_path(@project), method: :put, class: "form-horizontal" do %>
    <%
      rows = [
        [:delete_resource, "Delete resource", :boolean],
        [:replicas, "Replicas"],
        [:requests_cpu, 'Requests CPU Cores'],
        [:requests_memory, 'Requests Memory in MiB'],
        [:limits_cpu, 'Limits CPU Cores'],
        [:limits_memory, 'Limits Memory in MiB']
      ]
      rows << [:inject_istio_annotation, 'Istio sidecar', :boolean] if show_istio_sidecar_ui?

      row_builder = -> (field, type, attribute) { type == :boolean ? field.check_box(attribute) : field.text_field(attribute, size: 10) }
    %>
    <table class="table table-hover table-condensed">
      <thead>
        <tr>
          <th>Deploy Group</th>
          <th>Role</th>
          <% rows.each do |_, label| %>
            <th><%= label %></th>
          <% end %>
        </tr>
      </thead>

      <% @kubernetes_deploy_group_roles.each do |deploy_group_role| %>
        <% if deploy_group_role.errors.any? %>
          <tr>
            <td colspan="<%= 2 + rows.size %>"><%= render 'shared/errors', object: deploy_group_role %></td>
          </tr>
        <% end %>
        <tr>
          <td><%= deploy_group_role.deploy_group.name %></td>
          <td><%= deploy_group_role.kubernetes_role.name %></td>
          <%= fields_for "kubernetes_deploy_group_roles[#{deploy_group_role.id}]", deploy_group_role do |field| %>
            <% rows.each do |attribute, _, type| %>
              <td><%= row_builder.(field, type, attribute) %></td>
            <% end %>
          <% end %>
        </tr>
      <% end %>

      <tr class="set-all-row" style="background: light-gray">
        <td colspan="2">ALL</td>
        <%= fields_for "all", Kubernetes::DeployGroupRole.new do |field| %>
          <% rows.each do |attribute, _, type| %>
            <td><%= row_builder.(field, type, attribute) %></td>
          <% end %>
        <% end %>
      </tr>
    </table>

    <%= submit_tag "Update all" %>
  <% end %>
</section>
